import Vue from 'vue'
import VueRouter from 'vue-router'
//引入子路由
import role from '@/router/role'
import performance from '@/router/performance'
import talent from '@/router/talent'
import department from '@/router/department'
import staff from "@/router/tl/staff";
import resume from '@/router/resume'

import activity from '@/router/activity'
import target from '@/router/target'

import statistics from "@/router/Statistics";

Vue.use(VueRouter)

const routes = [
    ...resume,
    {
        path: '/',
        name: 'pLogin',
        redirect: '/longin',
    },
    {  //登录路由
        path: '/longin',
        name: 'longin',
        component: () => import('../views/Home/Login.vue')
    },

    {   //页面框架加子路由 meta元路由信息
        path: '/Home',
        name: 'Home',
        meta: {
            isLogin: true
        },
        component: () => import('../views/Home/HomeView.vue'),
        children: [

            ...role,
            ...performance,
            ...staff,
            ...talent,
            ...department,
            ...target,
            ...activity,
            ...statistics
        ],

    },

]


const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})


//路由的全局前置守卫

router.beforeEach(function (to, from, next) {
    if (to.matched.some(ele => ele.meta.isLogin)) {
        const token = localStorage.getItem('token')
        if (token) {
            next()

        } else {
            next('/longin')
        }
    } else {
        next()
    }

})

export default router
